var app = getApp()
var IP = app.globalData.IP
Page({

  /**
   * 页面的初始数据
   */
  data: {
    timer:'',
    // 用户昵称
    nickname: '',

    //用户头像
    headimageurl: '',


    //是否匿名
    anonymity: '匿名发布',

    comment: '',

    //上传文件指向的微信临时存储地址
    file: {},

    //图片展示url的数组
    fileList: [],

    //评分
    rate: 0,
    checked: true,

    //订单id
    orderid: '',

    openid: wx.getStorageSync('openid'),
    //订单的图片
    imageurl: '',
    serviceid: '',
    //订单id
    id: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {


    var that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('sendoptionstoevaluate', function (data) {
      that.setData({
        orderid: data.orderid,
        imageurl: data.imageurl,
        serviceid: data.serviceid,
        id: data.id
      })
      console.log(that.data.serviceid)



    })

    this.getuserheadimageurl()

  },


  //获取当前用户昵称头像
  getuserheadimageurl() {
    wx.request({
      url: `http://${IP}:8080/user/getUserheadImageurl`,
      data: {
        openid: this.data.openid
      },
      success: res => {
        this.setData({
          headimageurl: res.data.headimageurl,
          nickname: res.data.nickname
        })
        console.log(res.data)

      }
    })

  },

  //获取textarea的输入数据
  getTextarea(e) {
    this.setData({
      comment: e.detail.value
    })
    console.log(this.data.comment)
  },


  onChangecheckbox(event) {
    this.setData({
      checked: event.detail,
    });
    if (event.detail == true) {
      this.setData({

        anonymity: '匿名用户'
      })
    } else {
      this.setData({
        anonymity: this.data.nickname
      })
    }
  },

  changeStar(event) {
    this.setData({
      rate: event.detail,
    });

  },


  // 设置订单已完成
  updateStatusTo4(e) {
    wx.request({
      url: `http://${IP}:8080/order/updateStatusTo4/` + this.data.id,
      success: res => {

        this.onLoad()


      }

    })

  },
  //发布评价
  publish() {
    const {
      fileList
    } = this.data

    for (var i = 0; i <fileList.length; i++) {
      wx.uploadFile({
        filePath: fileList[i].url,
        name:'img',
        formData: {
          openid: this.data.openid,
          i: i + 1
        },
        url: `http://${IP}:8080/Comment/uploadCommentImage`
      })
    }


    wx.request({
      url: `http://${IP}:8080/Comment/saveComment`,
      data: {
        //订单id 评分 评价  是否匿名  openid
        orderid: this.data.orderid,
        rate: this.data.rate,
        comment: this.data.comment,
        nickname: this.data.anonymity,
        openid: this.data.openid,
        serviceid: this.data.serviceid,
        headimageurl: this.data.headimageurl,
      },
      success: res => {
        // console.log(res.data)
        this.updateStatusTo4()
      

      }
    })
    wx.showToast({
      title: '发布成功',
    })
    const timer = setTimeout(() => {
   
      wx.navigateBack()
    }, 1000)
    this.setData({
      timer:timer
    })
   


  },

  afterRead(event) {

    // file 对象格式
    const {
      file
    } = event.detail;

    // this.setData({
    //   file: file
    // })


    // 上传到临时存储  更新fileList
    const {
      //声明数组类型
      fileList = []
    } = this.data;

    //保存图片的临时缓存地址刷新图片列表
    fileList.push(file);

    this.setData({
      fileList: fileList
    });
    console.log(this.data.fileList)


  },
  deleteImage(e) {
    let currentindex = e.detail.index

    const {
      fileList
    } = this.data
    fileList.splice(currentindex, 1)

    this.setData({
      fileList: fileList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}





})